﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AcctrueWMS.Foundation.Data;
using System.Text.RegularExpressions;
using System.Data;

namespace AcctrueWMS.Foundation.Web
{
    class DataValidater
    {
        private IDataModelContext context = Runtime.Context;
        public DataValidater()
        {
            context = Runtime.Context;

        }

        /// <summary>
        ///  //FocusedTextBox验证数据库数据，
        /// </summary>
        /// <param name="modelName">模板名称</param>
        /// <param name="columnName">列名称</param>
        /// <param name="isClient">是否允许客户段自定义值: true 允许自定义，false：必须为存在值 默认为 允许，即true</param>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public bool ValidateData(string modelName, string columnName, string isClient, string keyValue)
        {
            keyValue = Microsoft.JScript.GlobalObject.unescape(keyValue);
            DataModel model = new DataModel(context, modelName);
            string dataExpr = ReplaceTokenWithValue(model.Columns[columnName].DataVolideExpr, keyValue);
            if (!Convert.ToBoolean(isClient))
            {
                if (!string.IsNullOrEmpty(dataExpr))
                {
                    object result = CommandExprFactory.Eval(dataExpr, context);
                    if (!string.IsNullOrEmpty(result.ToString()))
                    {
                        if (result is bool)
                        {
                            return Convert.ToBoolean(result);
                        }
                        else
                        {
                            DataTable dt = CommandExprFactory.Eval(dataExpr, context) as DataTable;
                            DataRow[] keyRows = dt.Select(string.Format("key = '{0}'", keyValue));
                            if (keyRows.Count() > 0)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                else
                {
                    return DefaultValideteData(model, columnName, keyValue);
                }
            }
            return DefaultValideteData(model, columnName, keyValue);

        }
        public bool DefaultValideteData(DataModel model, string columnName, string keyValue)
        {
            try
            {
                string defaultExpr = ReplaceTokenWithValue(model.Columns[columnName].Data, keyValue);
                object result = CommandExprFactory.Eval(defaultExpr, context);
                if (result is bool)
                {
                    return Convert.ToBoolean(result);
                }
                else
                {
                    DataTable dt = CommandExprFactory.Eval(defaultExpr, context) as DataTable;
                    DataRow[] keyRows = dt.Select(string.Format("key = '{0}'", keyValue));
                    if (keyRows.Count() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }
        }



        private string ReplaceTokenWithValue(string expr, string keyValue)
        {
            try
            {
                //分析匹配的可能参数，并进行替换。列参数以#开头
                Regex colname_reg = new Regex(@"{(?<colname>[\w|\d]+)}\s*");
                foreach (Match m in colname_reg.Matches(expr))
                {
                    string colName = m.Groups["colname"].Value;
                    expr = expr.Replace(m.Value, keyValue);
                }
                return expr;
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}
